/**
 * 
 */
package fr.miage.client.presentation.top;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.miage.client.metier.client.ClientBusinessService;
import fr.miage.client.metier.client.ClientBusinessServiceAsync;
import fr.miage.client.metier.entite.Client;
import fr.miage.client.presentation.LightBoxDialog;
import fr.miage.dao.traitements.ClientDao;

/**
 * @author romain, vincent
 *
 */
public class Authentification {
	ServiceDefTarget endpoint;
	ClientBusinessServiceAsync clientService;


	public Authentification() {
	}
	
	
	/**
	 * Initialize this example.
	 */
	
	
	public Widget onInitialize() {
		// Create the dialog box
		final LightBoxDialog LightBoxDialog = createLightBoxDialog();
		LightBoxDialog.setAnimationEnabled(true);
		
		// Create a button to show the dialog Box
		Button openButton = new Button("Authentification",
				new ClickListener() {
			public void onClick(Widget sender) {
				LightBoxDialog.center();
				LightBoxDialog.show();
			}
		});
		openButton.setWidth("180");
				
		// Add the button and list to a panel
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSpacing(8);
		vPanel.add(openButton);
		// Return the panel
		return vPanel;
	}
	
	/**
	 * Create the dialog box for this example.
	 * 
	 * @return the new dialog box
	 */
	
	private LightBoxDialog createLightBoxDialog() {
		final LightBoxDialog LightBoxDialog = new LightBoxDialog();
		LightBoxDialog.ensureDebugId("cwLightBoxDialog");
		LightBoxDialog.setText("Authentification");
		
		// Create a table to layout the content
		VerticalPanel dialogContents = new VerticalPanel();
		dialogContents.setSpacing(4);
		LightBoxDialog.setWidget(dialogContents);
		// Table dans laquelle on place les éléments
		final FlexTable flexTable = new FlexTable();
		dialogContents.add(flexTable);
		

		flexTable.setHTML(0, 0, "Email : ");
		final TextBox email = new TextBox();
		flexTable.setWidget(0, 1, email);
		
		flexTable.setHTML(1, 0, "Mot de passe : ");
		final PasswordTextBox password = new PasswordTextBox();
		flexTable.setWidget(1, 1, password); 
		
		/*flexTable.setHTML(2, 0, "Admin : ");
		final CheckBox admin = new CheckBox();
		flexTable.setWidget(2, 1, admin); 
		*/
		
		// Bouton de validation
		final Button validateButton = new Button();
		flexTable.setWidget(3, 1, validateButton);
		validateButton.setText("Valider");
				
		validateButton.addClickListener(
				new ClickListener() {
					public void onClick(Widget sender) {
						// création du proxy 
						
						clientService = (ClientBusinessServiceAsync) GWT.create(ClientBusinessService.class);
						endpoint = (ServiceDefTarget) clientService;
						String moduleRelativeURL = GWT.getModuleBaseURL() + "client";
						endpoint.setServiceEntryPoint(moduleRelativeURL);
						AsyncCallback callback = new AsyncCallback(){
							public void onSuccess(Object result) {  
								Client cNouv = (Client) result;
								
								if (cNouv == null){
									Window.alert("Ce compte n'existe pas, veuillez réessayer");
								}
								else{
									Window.alert("Vous êtes correctement identifié - pas encore logué");
									LightBoxDialog.hide();
								}					
							}							
							public void onFailure(Throwable caught) {
								System.out.println("callback auth fail");							
							}
						};
				
						Client cIn = new Client();
						cIn.setIdClient(5);
						cIn.setEmail(email.getText());
						clientService.getClient(cIn,callback);																		
					}
				});
		
		final Button closeButton = new Button();
  		flexTable.setWidget(4, 3, closeButton);
  		closeButton.setText("Annuler");
  		closeButton.addClickListener(new ClickListener(){
				public void onClick(Widget sender) {
					//if(Window.confirm("Etes-vous sur de vouloir fermer la fenêtre d'authentification")){
						email.setText("");
						password.setText("");
						LightBoxDialog.hide();
					//}					
				}
  			
  		});
		dialogContents.add(validateButton);
		dialogContents.add(closeButton);
		
		// Return the dialog box
		return LightBoxDialog;
	}
}
